//TODO: Grey out field where no queen can be placed anymore, because other queens are already there

package com.googlecode.eightqueenshaggle;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class BoardViewImageAdapter extends BaseAdapter {
    
	private Context mContext;

    public BoardViewImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
    	
    	return 	((BoardViewController)mContext).puzzle.dim1*
    			((BoardViewController)mContext).puzzle.dim2;
    	
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        
    	ImageView imageView;
        
    	if (convertView == null) {  // if it's not recycled, initialize some attributes
            
    		imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(30, 30));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);
        
    	} else {
            imageView = (ImageView) convertView;
        }


    	//TODO: Use function in BoardView Controller instead to figure out, if a cell is dark/bright
    	/*int dim1 = ((BoardViewController)mContext).puzzle.dim1;
    	int dim2 = ((BoardViewController)mContext).puzzle.dim2;
    	
    	int col_nr = position % dim2;
    	int row_nr = (position - col_nr) / dim2;

    	int tid;	
    	if(col_nr % 2 == 0 && row_nr % 2 == 0 || col_nr % 2 == 1 && row_nr % 2 == 1) //this is a dark cell
    		imageView.setImageResource(mThumbIds[1]);
    	else
    		imageView.setImageResource(mThumbIds[0]);*/
    	
    	
		//determine whether we need to draw a bright/dark cell
    	Coord logicalPosition = ((BoardViewController)mContext).gridPos2LogicalPos(position);
    	if (((BoardViewController)mContext).isDark(logicalPosition.x, logicalPosition.y))
    		imageView.setImageResource(R.drawable.field_empty_dark);
		else
			imageView.setImageResource(R.drawable.field_empty_bright);
        
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.field_empty_bright, 
            R.drawable.field_empty_dark,
            R.drawable.field_occupied_bright,
            R.drawable.field_occupied_dark
    };

	
}